package com.example.testapp.activity.activity.meact;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.testapp.R;
import com.example.testapp.activity.helper.AlbumHelper;
import com.example.testapp.activity.model.ImageItem;
import com.example.testapp.activity.view.bitmap.BitmapCache;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by l on 2016/7/5.
 */
public class ImageGridActivity extends AppCompatActivity {

    public static final String EXTRA_IMAGE_LIST = "imageList";

    private TextView mMenuTV, mCompleteTV;
    private GridView gridView = null;
    private ArrayList<ImageItem>  dataList;
    private ImageGridAdapter mAdapter;
    private AlbumHelper  helper;
    private RelativeLayout  mBottomLayout = null;

    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    Toast.makeText(ImageGridActivity.this, "最多选择9张图片", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_album);

        helper = AlbumHelper.getHelper();
        helper.init(getApplicationContext());
        dataList = (ArrayList<ImageItem>) getIntent().getSerializableExtra(EXTRA_IMAGE_LIST);

        initView();
    }

    private void initView() {
        mMenuTV = (TextView)findViewById(R.id.menuTV);
        mCompleteTV = (TextView)findViewById(R.id.completeTV);
        mBottomLayout = (RelativeLayout)findViewById(R.id.buttomLayout);
        mMenuTV.setOnClickListener(mOnClickListener);
        mCompleteTV.setOnClickListener(mOnClickListener);

        entryTitleStyle(false);
        gridView = (GridView)findViewById(R.id.gridItem);
        mAdapter = new ImageGridAdapter(this, dataList);
        gridView.setAdapter(mAdapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mAdapter.isSel){
                    mAdapter.mListItem.get(position).setSelected(!mAdapter.mListItem.get(position).isSelected());
                    mAdapter.notifyDataSetChanged();
                    updateCount(getSelcount());
                }else {
                    Intent intent = new Intent(ImageGridActivity.this, ImagePagerActivity.class);
                    //intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_LIST, (Serializable) dataList.get(position).ItemSelected);
                    startActivity(intent);
                }
            }
        });

        gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                if (mAdapter.isSel = true){
                    mAdapter.mListItem.get(position).setSelected(true);
                    mAdapter.notifyDataSetChanged();
                }
                updateCount(getSelcount());
                entryTitleStyle(true);
                return true;
            }
        });
    }

    private void updateCount(int count) {
        mCompleteTV.setText("("+ count + ")" + "完成");
    }

    private int getSelcount() {
        int count = 0;
        for(ImageItem item : mAdapter.mListItem){
            if(item.isSelected())
               count++;
        }
        return count;
    }

    private void entryTitleStyle(boolean show) {
        mBottomLayout.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           switch (v.getId()){
               case R.id.menuTV:

                   break;
           }
        }
    };

    public static class ImageGridAdapter extends BaseAdapter{
        final String TAG = getClass().getSimpleName();

        //private TextCallback textCallback = null;
        private List<ImageItem>  mListItem;
        //private Map<String, String>  map = new HashMap<>();
        private Activity  activity;
        private Handler   mHandler;
        private BitmapCache  cache;
        private int selectTotal = 0;
        private boolean  isSel = false;

        BitmapCache.ImageCallback callback = new BitmapCache.ImageCallback() {
            @Override
            public void imageLoad(ImageView imageView, Bitmap bitmap, Object... params) {
                 if (imageView != null && bitmap != null){
                    String url = (String) params[0];
                    if(url != null && url.equals((String) imageView.getTag())){
                        ((ImageView)imageView).setImageBitmap(bitmap);
                    }else{
                        Log.e(TAG, "callback, bmp not match");
                    }
                 }else {
                     Log.e(TAG, "callback, bmp null");
                 }
            }
        };

        public ImageGridAdapter(Activity act, List<ImageItem> list) {
            this.activity = act;
            this.mListItem = list;
            //this.mHandler = handler;
            cache = new BitmapCache();
        }

        /*public interface TextCallback{
            public void onListen(int count);
        }

        public void setTextCallback(TextCallback textCallback) {
            this.textCallback = textCallback;
        }*/

        @Override
        public int getCount() {
            //int count = 0;
            if (mListItem != null){
                return  mListItem.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return mListItem.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null){
                holder = new ViewHolder();
                convertView = View.inflate(activity, R.layout.view_item_grid, null);
                holder.mSelectImage = (ImageView) convertView.findViewById(R.id.image);
                holder.mCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }

            ImageItem item = mListItem.get(position);
            holder.mSelectImage.setTag(item.imagePath);
            cache.displayBitmap(holder.mSelectImage, item.thumbnailPath, item.imagePath, callback);

            if (isSel){
                holder.mCheckBox.setVisibility(View.VISIBLE);
                if (mListItem.get(position).isSelected()){
                    holder.mCheckBox.setVisibility(View.VISIBLE);
                    holder.mCheckBox.setChecked(true);
                }else {
                    holder.mCheckBox.setVisibility(View.GONE);
                    holder.mCheckBox.setChecked(false);
                }
            }else {
                holder.mCheckBox.setVisibility(View.GONE);
            }
            return convertView;
        }

        public class ViewHolder{
           public ImageView mSelectImage;
           public CheckBox  mCheckBox;
        }
    }
}
